package leetcode;

public class Test1582 {
    public int numSpecial(int[][] mat) {
        int[] rowMap = new int[mat.length];
        int[] colMap = new int[mat[0].length];
        //行扫描
        for (int row = 0; row < mat.length; row++) {
            int count = 0;
            for (int col = 0; col < mat[row].length; col++) {
                if (mat[row][col] == 1) {
                    ++count;
                }
            }
            rowMap[row] = count;//第row行有count个1
        }
        //列扫描
        for (int col = 0; col < mat[0].length; col++) {
            int count = 0;
            for (int row = 0; row < mat.length; row++) {
                if (mat[row][col] == 1) {
                    ++count;
                }
            }
            colMap[col] = count;
        }
        //扫描矩阵
        int count = 0;
        for (int row = 0; row < mat.length; row++) {
            if(rowMap[row]>1)
                continue;
            for (int col = 0; col < mat[row].length; col++) {
                //如果为1，判断行列是否只有一个1
                if (mat[row][col] == 1 && rowMap[row] == 1 && colMap[col] == 1) {
                    ++count;
                    break;
                }
            }
        }

        return count;
    }
}
